﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

using Entity.Package;
using System.Data;
using Tools;
namespace BLL.Business
{
    public class SearchUserInfo
    {
        /// <summary>
        /// 处理用户查询请求包
        /// </summary>
        /// <param name="search"></param>
        public static void GetGasUserInfo(PackRequestSearch search, CSocket.ClientThread client)
        {
            try
            { 
                //判断用户是否正在扣款
                DataTable dtIsPayment = Db.SearchInfoManager.GetKouKuanFlag(search.GasUserID.Trim());
                if ( dtIsPayment==null || dtIsPayment.Rows.Count == 0)
                {
                    PackReturnSearchSuccess pkSearchReturn = new PackReturnSearchSuccess();
                   
                    pkSearchReturn.TradeCode = ((int)TradeCode.缴费查询).ToString();
                    pkSearchReturn.ResponseCode = ((int)ResponseCode.该用户不存在).ToString().PadLeft(3, '0');
                    pkSearchReturn.PackHead = new PackHead(search.packHead.PayWay, search.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkSearchReturn.ToString()).Length-1).ToString());
                    byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkSearchReturn);
                    if (client.Send(sendPackage))  //发送返回包
                        client.CloseSocket();  //断开链接

                    LogHelper.WriteInfoLog("查询无此用户");
                    return;
                }                

                string koukuanFlag = dtIsPayment.Rows[0][0].ToString();
                if (koukuanFlag != "0")
                {
                    if (koukuanFlag == "1" || koukuanFlag == "2" || koukuanFlag == "3" || koukuanFlag == "13")  //1:POS机扣款；2：银行代扣; 3:网银正在缴费
                    {
                        PackReturnSearchSuccess pkSearchReturn = new PackReturnSearchSuccess();                       
                        pkSearchReturn.TradeCode = ((int)TradeCode.缴费查询).ToString();
                        if (koukuanFlag == "13")
                            pkSearchReturn.ResponseCode = ((int)ResponseCode.有预交费未写卡记录).ToString().PadLeft(3, '0');
                        else
                            pkSearchReturn.ResponseCode = ((int)ResponseCode.特殊用户不能在银行缴费).ToString().PadLeft(3, '0');
                        pkSearchReturn.PackHead = new PackHead(search.packHead.PayWay, search.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkSearchReturn.ToString()).Length-1).ToString());
                        byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkSearchReturn);
                        if (client.Send(sendPackage))
                            client.CloseSocket();                       
                        return;
                    }
                }
                else
                {
                    DataSet ds = Db.SearchInfoManager.GetPaymentInfoByUserID(search.GasUserID.Trim(),
                                                                               Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["ZNJDays"].ToString()));
                    if (ds == null || ds.Tables[0].Rows.Count<=0)  //查询失败
                    {
                        PackReturnSearchSuccess pkSearchReturn = new PackReturnSearchSuccess();
                      
                        pkSearchReturn.TradeCode = ((int)TradeCode.缴费查询).ToString();
                        pkSearchReturn.ResponseCode = ((int)ResponseCode.该用户不存在).ToString().PadLeft(3,'0');
                        pkSearchReturn.PackHead = new PackHead(search.packHead.PayWay,search.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkSearchReturn.ToString()).Length-1).ToString());
                        byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkSearchReturn);
                        if (client.Send(sendPackage))  //发送返回包
                            client.CloseSocket();  //断开链接

                        LogHelper.WriteInfoLog(string.Format("{0}用户信息查询失败！响应码：{1}", search.GasUserID.Trim(), ((int)Business.ResponseCode.该用户不存在).ToString()));
                    }
                    else  //查询成功，组织成功返回包
                    {
                        PackReturnSearchSuccess pkSearchReturn = new PackReturnSearchSuccess();
                        pkSearchReturn.TradeCode = ((int)TradeCode.缴费查询).ToString();
                        pkSearchReturn.ResponseCode = ((int)ResponseCode.交易成功).ToString().PadLeft(3,'0');
                        pkSearchReturn.OwnFeeMoney = ds.Tables[0].Rows[0]["OwnFeeMoney"].ToString();
                        pkSearchReturn.GasUserID = ds.Tables[0].Rows[0]["GasUserID"].ToString();
                        pkSearchReturn.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                        pkSearchReturn.Address = ds.Tables[0].Rows[0]["Address"].ToString();
                        pkSearchReturn.ProtocolType = "0";
                        pkSearchReturn.LastYE = ds.Tables[0].Rows[0]["AccountBalance"].ToString();
                        pkSearchReturn.UserProperties = "0";                     
                        pkSearchReturn.Memo = ds.Tables[0].Rows[0]["MeterType"].ToString();                      
                        pkSearchReturn.ExtraFee1 = ds.Tables[0].Rows[0]["MaxMoney"].ToString();
                        pkSearchReturn.Penalty = ds.Tables[0].Rows[0]["ZNJ"].ToString();                       
                        pkSearchReturn.LastMeterNumber = "";
                        pkSearchReturn.ThisMeterNumber = "";                       
                        pkSearchReturn.Aggregate = ds.Tables[0].Rows[0]["YJHJ"].ToString();
                        pkSearchReturn.CbRecordTimes = "0";
                        pkSearchReturn.PackHead = new PackHead(search.packHead.PayWay, search.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkSearchReturn.ToString()).Length).ToString());

                        LogHelper.WriteInfoLog("查询结果：" + pkSearchReturn.ToString());
                        byte[] sendPackage = CSocket.PackageHandle.CombinationPackage(pkSearchReturn);
                        if (client.Send(sendPackage))  //返回包发送成功，关闭链接
                            client.CloseSocket();

                        LogHelper.WriteInfoLog(string.Format("{0}用户信息查询成功！响应码：{1}", search.GasUserID.Trim(), ((int)Business.ResponseCode.交易成功).ToString()));

                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog("GetUserPaymentInfo方法-用户查询缴费信息出现异常，异常信息：" + ex.Message+ex.StackTrace,ex);
                PackFailReturn pkFailReturn = new PackFailReturn();
                pkFailReturn.TradeCode = ((int)TradeCode.缴费查询).ToString();
                pkFailReturn.ResponseCode = ((int)ResponseCode.其他错误).ToString().PadLeft(3, '0');
                pkFailReturn.PackHead = new PackHead(search.packHead.PayWay, search.packHead.BankWaterNumber, (Encoding.Default.GetBytes(pkFailReturn.ToString()).Length).ToString()); 
                CSocket.PackageHandle.SendFailurPackage(pkFailReturn,client);
            }
           
        }
    }
}
